package com.common.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.common.component.Pagination;
import com.common.manager.Role2DataManager;
import com.common.manager.Role2MenuFuncManager;
import com.common.manager.Role2MenuManager;
import com.common.manager.RoleInfoManager;
import com.common.manager.StaffManager;
import com.common.manager.SystemInfoManager;
import com.common.model.RoleInfoModel;
import com.framework.model.IData;
import com.framework.model.impl.DataMap;
import com.framework.web.BaseController;

@Controller
@RequestMapping(value = "/management/role")
public class RoleInfoController extends BaseController{

	 private static String modelUrl="/management/role/";
	 
    @Autowired
	private RoleInfoManager roleInfoManager;
    
    
    
    @Autowired
	private Role2MenuFuncManager role2MenuFuncManager;
    
    @Autowired
	private Role2MenuManager role2MenuManager;
    
    @Autowired
	private Role2DataManager role2DataManager;
    
    
    @Autowired
	private SystemInfoManager systemInfoManager;
    
    @Autowired
	private StaffManager staffManager;
    

    /**
     * 功能：页面分页列表
     * @param vo
     * @param param
     * @return
     */
	@RequestMapping("pageList")
	public ModelAndView pageList(@ModelAttribute("page") Pagination vo,@ModelAttribute("roleInfoModel") RoleInfoModel param) {
		ModelAndView modelView = new ModelAndView(modelUrl+"list");
		List<DataMap> dataList = roleInfoManager.getPageObjectList(vo, param);
		modelView.addObject("vo", vo);
		modelView.addObject("param", param);
		modelView.addObject("objList", dataList);
		return modelView;
	}
	
	/**
	 * 功能：初始化添加页面
	 * @param roleInfoModel
	 * @return
	 */
	@RequestMapping("initAdd")
	public ModelAndView initAdd(@ModelAttribute("roleInfoModel") RoleInfoModel roleInfoModel) {
		ModelAndView modelView = new ModelAndView(modelUrl+"add");
		log.info("add is page");
		this.initSystemCode(modelView);
		modelView.addObject("vo",roleInfoModel);
		return modelView;
	}
	
	/**
	 * 功能：初始化编辑页面
	 * @param intID
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/{intID}/initEdit")
	public ModelAndView initEdit(@PathVariable("intID") String intID) {
		ModelAndView modelView = new ModelAndView(modelUrl+"update");
		IData dataMap = new DataMap();
		dataMap.put("intID", intID);
		RoleInfoModel roleInfoModel = (RoleInfoModel)roleInfoManager.getObjectInfo(dataMap);
		this.initSystemCode(modelView);
		modelView.addObject("vo",roleInfoModel);
		return modelView;
	}
	
	
	private void initSystemCode(ModelAndView modelView){
		List systemList = systemInfoManager.getObjectList(new DataMap());
		modelView.addObject("systemList", systemList);
	}
	
	/**
	 * 功能：执行插入操作
	 * @param roleInfoModel
	 * @return
	 */
	@RequestMapping("add")
	public Object insert(HttpServletResponse response,@ModelAttribute("roleInfoModel") RoleInfoModel roleInfoModel) {
		try{
			int opNum = roleInfoManager.insertObjectAndGetID(roleInfoModel);
			if(opNum>0){
				return super.jsonReturn(response,200,true);
			}else{
				return super.jsonReturn(response,300,true);
			}
		} catch (Exception e) {
			return super.jsonReturn(response,300,e.getMessage(),true);
		}
	}
	
	/**
	 * 功能：执行更新操作
	 * @param roleInfoModel
	 * @return
	 */
	@RequestMapping("update")
	public Object update(HttpServletResponse response,@ModelAttribute("roleInfoModel") RoleInfoModel roleInfoModel) {
		try{
			int opNum =roleInfoManager.updateObject(roleInfoModel);
			if(opNum>0){
				return super.jsonReturn(response,200,true);
			}else{
				return super.jsonReturn(response,300,true);
			}
		} catch (Exception e) {
			return super.jsonReturn(response,300,e.getMessage(),true);
		}
	}
	
	/**
	 * 功能：执行单记录删除
	 * @param ids
	 * @return
	 */
	@RequestMapping("/{intID}/delete")
	public Object delete(HttpServletResponse response,@PathVariable("intID") int intID) {
		try{
			IData dataMap = new DataMap();
			dataMap.put("ids", intID);
			List staffList =staffManager.getObjectList(dataMap);
			if(staffList!=null && staffList.size()>0){
				return super.jsonReturn(response,300,"存在用户绑定了该角色,删除失败!",false);
			}else{
				dataMap.put("roleId", intID);
				int opNum =roleInfoManager.delAllRoleRelation(dataMap);
				if(opNum>0){
					return super.jsonReturn(response,200,false);
				}else{
					return super.jsonReturn(response,300,false);
				}
			}
			
		} catch (Exception e) {
			return super.jsonReturn(response,300,e.getMessage(),false);
		}
	}
	
	/**
	 * 功能：执行批量删除
	 * @param ids
	 * @return
	 */
	@RequestMapping("/deleteAll")
	public Object deleteAll(HttpServletResponse response,HttpServletRequest request) {
		try{
			String ids = request.getParameter("ids");
			IData dataMap = new DataMap();
			dataMap.put("ids", ids);
			String message="";
			List staffList =staffManager.getObjectList(dataMap);
			if(staffList!=null && staffList.size()>0){
				return super.jsonReturn(response,200,"存在用户绑定了该角色,删除失败!",false);
			}else{
				
				int opNum =roleInfoManager.deleteObject("deleteAllObject",dataMap);
				if(opNum>0){
					return super.jsonReturn(response,200,false);
				}else{
					return super.jsonReturn(response,300,false);
				}
			}
			
			
		} catch (Exception e) {
			return super.jsonReturn(response,300,e.getMessage(),false);
		}
	}
	
	
	/**
	 * 功能：显示角色菜单树
	 * @param response
	 * @param intID
	 * @return
	 */
	@RequestMapping("/{intID}/setRole2Menu")
	public ModelAndView setRole2Menu(HttpServletResponse response,@PathVariable("intID") int intID) {
		ModelAndView modelView = new ModelAndView(modelUrl+"role2MenuTree");
		log.info("roleBindlist is page");
		modelView.addObject("roleId", intID);
		return modelView;
	}
	
	
	/**
	 * 功能：保存角色菜单功能信息
	 * @param response
	 * @param request
	 * @return
	 */
	@RequestMapping("/saveRoleMenus")
	public Object saveRoleMenus(HttpServletResponse response,HttpServletRequest request){
		String roleId = request.getParameter("roleId");
		//清楚角色对应信息
		IData conditionObj = new DataMap();
		conditionObj.put("roleId", roleId);
		role2MenuManager.deleteObject(conditionObj);
		role2MenuFuncManager.deleteObject(conditionObj);
		
		String menuJson = request.getParameter("roleMenuFunc");
		JSONArray menuArray = JSONArray.fromObject(menuJson);
		for (Object object : menuArray) {
			JSONObject menuObj = (JSONObject)object;
			if("M".equals(menuObj.get("typeFlag"))){
				conditionObj.put("menuId",  menuObj.get("intID"));
				conditionObj.put("menuName",  menuObj.get("name"));
				role2MenuManager.insertObject(conditionObj);
			}else if("F".equals(menuObj.get("typeFlag"))){
				conditionObj.put("menuId", menuObj.get("pId"));
				conditionObj.put("funcId", menuObj.get("intID"));
				conditionObj.put("funcCode", menuObj.get("id"));
				role2MenuFuncManager.insertObject(conditionObj);
			}
		}
		
		return super.jsonReturn(response,200,true);
		
	}
	
	
	/**
	 * 显示角色机构树
	 * @param response
	 * @param intID
	 * @return
	 */
	@RequestMapping("/{intID}/setRole2Org")
	public ModelAndView setRole2Org(HttpServletResponse response,@PathVariable("intID") int intID) {
		ModelAndView modelView = new ModelAndView(modelUrl+"role2OrgTree");
		modelView.addObject("roleId", intID);
		return modelView;
	}
	
	/**
	 * 保存角色机构树
	 * @param response
	 * @param request
	 * @return
	 */
	@RequestMapping("/saveRoleOrg")
	public Object saveRoleOrg(HttpServletResponse response,HttpServletRequest request){
		String roleId = request.getParameter("roleId");
		//清楚角色对应信息
		IData conditionObj = new DataMap();
		conditionObj.put("roleId", roleId);
		role2DataManager.deleteObject(conditionObj);
		
		String selCheckJson = request.getParameter("roleSelOrg");
		JSONArray orgArray = JSONArray.fromObject(selCheckJson);
		for (Object object : orgArray) {
			JSONObject selObj = (JSONObject)object;
			conditionObj.put("orgCode",  selObj.get("id"));
			conditionObj.put("orgName",  selObj.get("name"));
			role2DataManager.insertObject(conditionObj);
		}
		return super.jsonReturn(response,200,true);
	}
}